using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using secret_server.Data;
using secret_server.Models;
using secret_server.ViewModels;
using Microsoft.AspNetCore.Authorization;
using System.IO;

namespace secret_server.Controllers
{
    [Authorize(Roles = "Admin")]
    public class WorldsController : Controller
    {
        private readonly SecretDbContext _context;

        public WorldsController(SecretDbContext context)
        {
            _context = context;
        }

        // GET: Worlds
        public async Task<IActionResult> Index()
        {
            return View(await _context.Worlds.ToListAsync());
        }

        // GET: Worlds/Details/5
        public async Task<IActionResult> Details(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var world = await _context.Worlds
                .SingleOrDefaultAsync(m => m.Id == id);
            if (world == null)
            {
                return NotFound();
            }

            return View(world);
        }

        // GET: Worlds/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: Worlds/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Title,StartDate,BackgroundForm")] WorldForm worldform)
        {
            World world = (World)worldform;
            using (var memoryStream = new MemoryStream())
            {
                await worldform.BackgroundForm.CopyToAsync(memoryStream);
                world.Background = memoryStream.ToArray();
            }
            if (ModelState.IsValid)
            {
                world.Id = Guid.NewGuid();
                _context.Add(world);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(world);
        }

        // GET: Worlds/Edit/5
        public async Task<IActionResult> Edit(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var world = await _context.Worlds.SingleOrDefaultAsync(m => m.Id == id);
            if (world == null)
            {
                return NotFound();
            }
            return View(world);
        }

        // POST: Worlds/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(Guid id, [Bind("Id,Title,StartDate,BackgroundForm")] WorldForm worldform)
        {
            if (id != worldform.Id)
            {
                return NotFound();
            }

            World world = (World)worldform;
            if (ModelState.IsValid)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await worldform.BackgroundForm.CopyToAsync(memoryStream);
                    world.Background = memoryStream.ToArray();
                }

                try
                {
                    _context.Update(world);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!WorldExists(world.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(world);
        }

        // GET: Worlds/Delete/5
        public async Task<IActionResult> Delete(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var world = await _context.Worlds
                .SingleOrDefaultAsync(m => m.Id == id);
            if (world == null)
            {
                return NotFound();
            }

            return View(world);
        }

        // POST: Worlds/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(Guid id)
        {
            var world = await _context.Worlds.SingleOrDefaultAsync(m => m.Id == id);
            _context.Worlds.Remove(world);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool WorldExists(Guid id)
        {
            return _context.Worlds.Any(e => e.Id == id);
        }
    }
}
